import $validate from '@/utils/open/validate'
// import Layout from '@/views/layout/Layout'
// import layout from '@/components/layout'
import layout from '@/views/first'
import $com from '@/utils/open/common'

export default {
  /**
   * 设置树级别
   * @param tree
   * @param level
   * @param childName
   * @returns {*}
   */
  setTreeLevel(tree, level, childName) {
    this.ergodicTreeLevel(tree, level, childName)
    return tree
  },
  /**
   * 遍历并设置树级别
   * @param tree
   * @param level
   * @param childName
   */
  ergodicTreeLevel(tree, level, childName) {
    tree.forEach(element => {
      element.level = level + 1
      element[childName] && this.ergodicTreeLevel(element[childName], element.level, childName)
    })
  },
  /**
   * 格式化路由
   * @param aMenu
   * @param pid
   * @param pPath
   * @returns {Array}
   */
  formatRoutes(aMenu) {
    debugger
    const aRouter = []
    aMenu.forEach(oMenu => {
      const { path, component, name, icon, children, isShow } = oMenu
      // if (!$validate.isNull(component)) {
      const oRouter = {
        path: path,
        name: name,
        meta: {
          icon: icon,
          title: name
        },
        isShow: isShow,
        children: $validate.isNull(children) ? [] : this.formatRoutes(children)
      }
      if (component === '' || component === 'Home') {
        oRouter.component = layout
        oRouter.path = '/' + path
      } else {
        oRouter.component = (resolve) => require([`../../${component}.vue`], resolve)
        oRouter.path = path
      }

      if (oMenu.accessType === $com.static.accessType.out && oMenu.type === '3') { // 字典外部链接
        // debugger
        delete oRouter.component
        delete oRouter.name
      }
      if (oMenu.isShow === $com.static.isShow.hide) {
        oRouter.hidden = true
      }

      aRouter.push(oRouter)
      // }
    })
    return aRouter
  },
  /**
   * 获取一级导航
   * @param that
   * @returns {default.computed.menuList|Array}
   */
  getMenuList(that) {
    let menuList = that.$store.state.permission.menuList
    if (!menuList || menuList.length <= 0) {
      menuList = $com.getCache('menuList')
      that.$store.dispatch('permission/updateMenuList', menuList)
    }
    return menuList
  }

}
